/**   
 * Copyright © 2016 公司名. All rights reserved.
 * 
 * @Title: PrivateKeyServiceImpl.java 
 * @Prject: LianniuManager
 * @Package: com.lianniu.mgr.privateKey.service.impl
 * @Description: TODO
 * @author: Administrator   
 * @date: 2016年11月10日 下午4:21:06 
 * @version: V1.0   
 */
package com.lianniu.mgr.privateKey.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.privateKey.dao.PrivateKeyDao;
import com.lianniu.mgr.privateKey.model.PrivateKeyVo;
import com.lianniu.mgr.privateKey.service.IPrivateKeyService;
import com.lianniu.mgr.util.PrivateKeyUtil;

/** 
 * @ClassName: PrivateKeyServiceImpl 
 * @Description: 秘钥Service实现
 * @author: lzz
 * @date: 2016年11月10日 下午4:21:06  
 */
@Service("privateKeyService")
public class PrivateKeyServiceImpl implements IPrivateKeyService {
    
    private final static Logger logger = Logger.getLogger(PrivateKeyServiceImpl.class);
    @Autowired
    private PrivateKeyDao privateKeyDao;
    /**
     * 获取邀请码
     */
    @Override
    @Transactional
    public BaseResult getSimplePrivateKey(String userId,String staffId, String staffName, Long staffMobile) {
        logger.info("getSimplePrivateKey begin");
        logger.info("privateKeyVo init begin");
        List<String> keyList = new ArrayList<>();
        PrivateKeyVo privateKeyVo = new PrivateKeyVo();
        String s = PrivateKeyUtil.getPricateKey();
        privateKeyVo.setCard(s);
        privateKeyVo.setAddUserId(userId);
        privateKeyVo.setCreateTime(new Date());
        privateKeyVo.setStaffId(staffId);
        privateKeyVo.setStaffName(staffName);
        privateKeyVo.setStaffMobile(staffMobile);
        keyList.add(s);
        logger.info("privateKeyVo init end  privateKeyVo:"+JSON.toJSONString(privateKeyVo));
        logger.info("getSimplePrivateKey createPrivateKey begin");
        try {
            Integer i = privateKeyDao.createPrivateKey(privateKeyVo);
            if(null != i){
                return BaseResult.success(keyList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("createPrivateKey Exception:"+"["+e.getMessage()+"]");
            return BaseResult.exception(e.getMessage());
        }
        logger.info("getSimplePrivateKey createPrivateKey end");
        
        return BaseResult.success();
    }
    @Override
    public BaseResult getListPrivateKey(PrivateKeyVo privateKeyVo, PaginationInfo paginationInfo) {
        logger.info("getListPrivateKey begin");
        logger.info("privateKeyVo init begin");
        privateKeyVo.setPaginationInfo(paginationInfo);
        logger.info("privateKeyVo init end");
        try {
            List<PrivateKeyVo> list = privateKeyDao.getPrivateKeyListForPage();
            logger.info("getListPrivateKey end");
            return BaseResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("getPrivateKeyListForPage Exception:"+"["+e.getMessage()+"]");
            logger.info("getListPrivateKey end");
            return BaseResult.exception(e.getMessage());
        }
    }
    @Override
    public BaseResult getQueryPrivateKey(String userId, String staffId, String staffName,
            Long staffMobile,int sum) {
        logger.info("getQueryPrivateKey begin");
        logger.info("List<PrivateKeyVo> init begin");
        List<PrivateKeyVo> list = new ArrayList<>();
        List<String> keyList = new ArrayList<>();
        for(int i =0;i<sum;i++){
            PrivateKeyVo privateKeyVo = new PrivateKeyVo();
            String s = PrivateKeyUtil.getPricateKey();
            privateKeyVo.setCard(s);
            privateKeyVo.setAddUserId(userId);
            privateKeyVo.setCreateTime(new Date());
            privateKeyVo.setStaffId(staffId);
            privateKeyVo.setStaffName(staffName);
            privateKeyVo.setStaffMobile(staffMobile);
            list.add(privateKeyVo);
            keyList.add(s);
        }
        logger.info("List<PrivateKeyVo> init end  List<PrivateKeyVo>:"+JSON.toJSONString(list));
        logger.info("getQueryPrivateKey createQueryPrivateKey begin");
        try {
            logger.info("getQueryPrivateKey destroyPrivateKeyStatus begin");
            privateKeyDao.destroyPrivateKeyStatus(staffId,2);
            logger.info("getQueryPrivateKey destroyPrivateKeyStatus end");
            Integer i = privateKeyDao.createQueryPrivateKey(list);
            if(null != i){
                return BaseResult.success(keyList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("createQueryPrivateKey Exception:"+"["+e.getMessage()+"]");
            return BaseResult.exception(e.getMessage());
        }
        logger.info("getQueryPrivateKey createQueryPrivateKey end");
        
        return BaseResult.success();
    }
    @Override
    public BaseResult getQueryPrivateKey(String staffId) {
        logger.info("getQueryPrivateKey begin");
        List<String> l = privateKeyDao.selectKeyByStaffId(staffId);
        logger.info("getQueryPrivateKey end");
        return BaseResult.success(l);
    }
    
    
}
